﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace _06.MultiValueDictionary
{
    class Program
    {
        static StringBuilder output = new StringBuilder();
        static MultiValueDictionary<int, int> dictionary = new MultiValueDictionary<int, int>();

        static void Main(string[] args)
        {
            //TestGenerator.CreateTest("test10.in.txt");
            //ReadInput(@"..\..\Tests\test10.in.txt");
            //PrintOutput(@"..\..\Tests\test10.out.txt");
            ReadInput();
            PrintOutput();
        }

        private static void ReadInput()
        {
            while (true)
            {
                string commandText = Console.ReadLine();
                if (commandText == "End" || commandText == null)
                {
                    break;
                }
                try
                {
                    ProcessCommand(commandText);
                }
                catch (Exception ex)
                {
                    output.Append("Unhandled exception: " + ex);
                }
            }

        }

        private static void ReadInput(string fileName)
        {
            StreamReader reader = new StreamReader(fileName);
            string line = reader.ReadLine();
            while (line != String.Empty && line != "End")
            {
                ProcessCommand(line);
                line = reader.ReadLine();
            }

        }

        private static void ProcessCommand(string commandText)
        {
            string[] command = commandText.Split(new string[] { " | " }, StringSplitOptions.RemoveEmptyEntries);
            switch (command[0])
            {
                case "Add":
                    {
                        string[] arguments = command[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        dictionary.Add(int.Parse(arguments[0]),int.Parse(arguments[1]));
                        break;
                    }
                case "Remove":
                    {
                        int key = int.Parse(command[1]);
                        if (dictionary.ContainsKey(key))
                        {
                            dictionary.Remove(int.Parse(command[1]));
                        }
                        break;
                    }
                case "ContainsKey":
                    {
                        output.AppendLine(dictionary.ContainsKey(int.Parse(command[1])).ToString());
                        break;
                    }

                case "Count":
                    {
                        output.AppendLine(dictionary.Count.ToString());
                        break;
                    }
                case "Clear":
                    {
                        dictionary.Clear();
                        break;
                    }
                case "this":
                    {
                        int key = int.Parse(command[1]);
                        if (dictionary.ContainsKey(key))
                        {
                            ICollection<int> list = dictionary[key];
                            output.Append(key + "->");
                            foreach (var value in list)
                            {
                                output.Append(value + ",");
                            }
                            output.Remove(output.Length - 1, 1);
                            output.AppendLine();
                        }
                        break;
                    }
                case "Foreach":
                    {
                        foreach (var item in dictionary)
                        {
                            output.Append(item.Key + "->");
                            foreach (var value in item.Value)
                            {
                                output.Append(value+",");
                            }
                            output.Remove(output.Length - 1, 1);
                            output.AppendLine();
                        }
                        break;
                    }
            }
        }

        private static void PrintOutput()
        {

            Console.WriteLine(output);
        }

        private static void PrintOutput(string fileName)
        {

            StreamWriter writer = new StreamWriter(fileName);
            using (writer)
            {
                writer.Write(output);
            }
        }

    }
}
